<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
<script src="../resources/input-modifiers.js"></script>
</head>
<body>
<script>

description("This tests the constructor for the KeyboardEvent DOM class.");

var testObject = {nyannyan: 123};

// No initializer is passed.
shouldBe("new KeyboardEvent('eventType').bubbles", "false");
shouldBe("new KeyboardEvent('eventType').cancelable", "false");
shouldBe("new KeyboardEvent('eventType').view", "null");
shouldBe("new KeyboardEvent('eventType').detail", "0");
shouldBe("new KeyboardEvent('eventType').location", "0");
shouldBe("new KeyboardEvent('eventType').ctrlKey", "false");
shouldBe("new KeyboardEvent('eventType').altKey", "false");
shouldBe("new KeyboardEvent('eventType').shiftKey", "false");
shouldBe("new KeyboardEvent('eventType').metaKey", "false");
shouldBe("new KeyboardEvent('eventType').repeat", "false");

// bubbles is passed.
shouldBe("new KeyboardEvent('eventType', { bubbles: false }).bubbles", "false");
shouldBe("new KeyboardEvent('eventType', { bubbles: true }).bubbles", "true");

// cancelable is passed.
shouldBe("new KeyboardEvent('eventType', { cancelable: false }).cancelable", "false");
shouldBe("new KeyboardEvent('eventType', { cancelable: true }).cancelable", "true");

// view is passed.
// Window objects.
shouldBe("new KeyboardEvent('eventType', { view: window }).view", "window");
shouldBe("new KeyboardEvent('eventType', { view: this }).view", "this");

// Non-window objects.
shouldThrow("new KeyboardEvent('eventType', { view: testObject }).view");
shouldThrow("new KeyboardEvent('eventType', { view: document }).view");
shouldBe("new KeyboardEvent('eventType', { view: undefined }).view", "null");
shouldBe("new KeyboardEvent('eventType', { view: null }).view", "null");
shouldThrow("new KeyboardEvent('eventType', { view: false }).view");
shouldThrow("new KeyboardEvent('eventType', { view: true }).view");
shouldThrow("new KeyboardEvent('eventType', { view: '' }).view");
shouldThrow("new KeyboardEvent('eventType', { view: 'chocolate' }).view");
shouldThrow("new KeyboardEvent('eventType', { view: 12345 }).view");
shouldThrow("new KeyboardEvent('eventType', { view: 18446744073709551615 }).view");
shouldThrow("new KeyboardEvent('eventType', { view: NaN }).view");
// Note that valueOf() is not called, when the left hand side is evaluated.
shouldThrow("new KeyboardEvent('eventType', { view: {valueOf: function () { return window; } } }).view == window");
shouldBe("new KeyboardEvent('eventType', { get view() { return window; } }).view", "window");
shouldThrow("new KeyboardEvent('eventType', { get view() { return 123; } }).view");
shouldThrow("new KeyboardEvent('eventType', { get view() { throw 'KeyboardEvent Error'; } })");

// code is passed.
// Strings.
shouldBeEqualToString("new KeyboardEvent('eventType', { code: 'koakuma' }).code", "koakuma");
shouldBeEqualToString("new KeyboardEvent('eventType', { code: '' }).code", "");

// Non-strings.
shouldBeEqualToString("new KeyboardEvent('eventType', { code: undefined }).code", "");
shouldBeEqualToString("new KeyboardEvent('eventType', { code: null }).code", "null");
shouldBeEqualToString("new KeyboardEvent('eventType', { code: false }).code", "false");
shouldBeEqualToString("new KeyboardEvent('eventType', { code: true }).code", "true");
shouldBeEqualToString("new KeyboardEvent('eventType', { code: 12345 }).code", "12345");
shouldBeEqualToString("new KeyboardEvent('eventType', { code: 18446744073709551615 }).code", "18446744073709552000");
shouldBeEqualToString("new KeyboardEvent('eventType', { code: NaN }).code", "NaN");
shouldBeEqualToString("new KeyboardEvent('eventType', { code: [] }).code", "");
shouldBeEqualToString("new KeyboardEvent('eventType', { code: [1, 2, 3] }).code", "1,2,3");
shouldBeEqualToString("new KeyboardEvent('eventType', { code: {koakuma: 12345} }).code", "[object Object]");
shouldBeEqualToString("new KeyboardEvent('eventType', { code: {valueOf: function () { return 'koakuma'; } } }).code", "[object Object]");

// key is passed.
// Strings.
shouldBeEqualToString("new KeyboardEvent('eventType', { key: 'koakuma' }).key", "koakuma");
shouldBeEqualToString("new KeyboardEvent('eventType', { key: '' }).key", "");

// Non-strings.
shouldBeEqualToString("new KeyboardEvent('eventType', { key: undefined }).key", "");
shouldBeEqualToString("new KeyboardEvent('eventType', { key: null }).key", "null");
shouldBeEqualToString("new KeyboardEvent('eventType', { key: false }).key", "false");
shouldBeEqualToString("new KeyboardEvent('eventType', { key: true }).key", "true");
shouldBeEqualToString("new KeyboardEvent('eventType', { key: 12345 }).key", "12345");
shouldBeEqualToString("new KeyboardEvent('eventType', { key: 18446744073709551615 }).key", "18446744073709552000");
shouldBeEqualToString("new KeyboardEvent('eventType', { key: NaN }).key", "NaN");
shouldBeEqualToString("new KeyboardEvent('eventType', { key: [] }).key", "");
shouldBeEqualToString("new KeyboardEvent('eventType', { key: [1, 2, 3] }).key", "1,2,3");
shouldBeEqualToString("new KeyboardEvent('eventType', { key: {koakuma: 12345} }).key", "[object Object]");
shouldBeEqualToString("new KeyboardEvent('eventType', { key: {valueOf: function () { return 'koakuma'; } } }).key", "[object Object]");

// location is passed.
// numbers within the unsigned long range.
shouldBe("new KeyboardEvent('eventType', { location: KeyboardEvent.DOM_KEY_LOCATION_STANDARD }).location", "KeyboardEvent.DOM_KEY_LOCATION_STANDARD");
shouldBe("new KeyboardEvent('eventType', { location: KeyboardEvent.DOM_KEY_LOCATION_LEFT }).location", "KeyboardEvent.DOM_KEY_LOCATION_LEFT");
shouldBe("new KeyboardEvent('eventType', { location: 4294967294 }).location", "4294967294");
shouldBe("new KeyboardEvent('eventType', { location: 4294967295 }).location", "4294967295");

// numbers out of the unsigned long range.
// 2^{53}-1, the largest number that can be exactly represented by double.
shouldBe("new KeyboardEvent('eventType', { location: 9007199254740991 }).location", "4294967295");
// 2^{64}-1
shouldBe("new KeyboardEvent('eventType', { location: 18446744073709551615 }).location", "KeyboardEvent.DOM_KEY_LOCATION_STANDARD");
shouldBe("new KeyboardEvent('eventType', { location: 12345678901234567890 }).location", "3944679424");
shouldBe("new KeyboardEvent('eventType', { location: -1 }).location", "4294967295");
shouldBe("new KeyboardEvent('eventType', { location: 123.45 }).location", "123");
shouldBe("new KeyboardEvent('eventType', { location: NaN }).location", "KeyboardEvent.DOM_KEY_LOCATION_STANDARD");

// Non-numeric values.
shouldBe("new KeyboardEvent('eventType', { location: undefined }).location", "KeyboardEvent.DOM_KEY_LOCATION_STANDARD");
shouldBe("new KeyboardEvent('eventType', { location: null }).location", "KeyboardEvent.DOM_KEY_LOCATION_STANDARD");
shouldBe("new KeyboardEvent('eventType', { location: '' }).location", "KeyboardEvent.DOM_KEY_LOCATION_STANDARD");
shouldBe("new KeyboardEvent('eventType', { location: '12345' }).location", "12345");
shouldBe("new KeyboardEvent('eventType', { location: '12345a' }).location", "KeyboardEvent.DOM_KEY_LOCATION_STANDARD");
shouldBe("new KeyboardEvent('eventType', { location: 'abc' }).location", "KeyboardEvent.DOM_KEY_LOCATION_STANDARD");
shouldBe("new KeyboardEvent('eventType', { location: [] }).location", "KeyboardEvent.DOM_KEY_LOCATION_STANDARD");
shouldBe("new KeyboardEvent('eventType', { location: [12345] }).location", "12345");
shouldBe("new KeyboardEvent('eventType', { location: [12345, 67890] }).location", "KeyboardEvent.DOM_KEY_LOCATION_STANDARD");
shouldBe("new KeyboardEvent('eventType', { location: {} }).location", "KeyboardEvent.DOM_KEY_LOCATION_STANDARD");
shouldBe("new KeyboardEvent('eventType', { location: {moemoe: 12345} }).location", "KeyboardEvent.DOM_KEY_LOCATION_STANDARD");
shouldBe("new KeyboardEvent('eventType', { location: {valueOf: function () { return 12345; }} }).location", "12345");

// ctrlKey, altKey, shiftKey, metaKey and repeat are passed.
["ctrlKey", "altKey", "shiftKey", "metaKey", "repeat"].forEach(function (attr) {
    shouldBe("new KeyboardEvent('eventType', { " + attr + ": false })." + attr, "false");
    shouldBe("new KeyboardEvent('eventType', { " + attr + ": true })." + attr, "true");
});

// all modifiers are passed correctly.
forEachModifier(function(attr, modifierName) {
    shouldBe("new KeyboardEvent('eventType', { " + attr + ": false }).getModifierState('" + modifierName + "');", "false");
    shouldBe("new KeyboardEvent('eventType', { " + attr + ": true }).getModifierState('" + modifierName + "');", "true");
});

// All initializers are passed.
shouldBe("new KeyboardEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, code: 'chocode', key: 'chokey', location: 222, ctrlKey: true, altKey: true, shiftKey: true, metaKey: true, repeat: true }).bubbles", "true");
shouldBe("new KeyboardEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, code: 'chocode', key: 'chokey', location: 222, ctrlKey: true, altKey: true, shiftKey: true, metaKey: true, repeat: true }).cancelable", "true");
shouldBe("new KeyboardEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, code: 'chocode', key: 'chokey', location: 222, ctrlKey: true, altKey: true, shiftKey: true, metaKey: true, repeat: true }).view", "window");
shouldBe("new KeyboardEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, code: 'chocode', key: 'chokey', location: 222, ctrlKey: true, altKey: true, shiftKey: true, metaKey: true, repeat: true }).detail", "111");
shouldBeEqualToString("new KeyboardEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, code: 'chocode', key: 'chokey', location: 222, ctrlKey: true, altKey: true, shiftKey: true, metaKey: true, repeat: true }).code", "chocode");
shouldBeEqualToString("new KeyboardEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, code: 'chocode', key: 'chokey', location: 222, ctrlKey: true, altKey: true, shiftKey: true, metaKey: true, repeat: true }).key", "chokey");
shouldBe("new KeyboardEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, code: 'chocode', key: 'chokey', location: 222, ctrlKey: true, altKey: true, shiftKey: true, metaKey: true, repeat: true }).location", "222");
shouldBe("new KeyboardEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, code: 'chocode', key: 'chokey', location: 222, ctrlKey: true, altKey: true, shiftKey: true, metaKey: true, repeat: true }).ctrlKey", "true");
shouldBe("new KeyboardEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, code: 'chocode', key: 'chokey', location: 222, ctrlKey: true, altKey: true, shiftKey: true, metaKey: true, repeat: true }).altKey", "true");
shouldBe("new KeyboardEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, code: 'chocode', key: 'chokey', location: 222, ctrlKey: true, altKey: true, shiftKey: true, metaKey: true, repeat: true }).shiftKey", "true");
shouldBe("new KeyboardEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, code: 'chocode', key: 'chokey', location: 222, ctrlKey: true, altKey: true, shiftKey: true, metaKey: true, repeat: true }).metaKey", "true");
shouldBe("new KeyboardEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, code: 'chocode', key: 'chokey', location: 222, ctrlKey: true, altKey: true, shiftKey: true, metaKey: true, repeat: true }).repeat", "true");
</script>
</body>
</html>
